package com.hornsun.repository;

import com.hornsun.configInfo.ConfigInfo;
import com.hornsun.domain.dbo.Verify;
import com.hornsun.domain.dbo.VerifyExample;
import com.hornsun.repository.mapper.VerifyMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.Date;

/**
 * Created by 编译中... on 2018/5/29.
 */
@Repository
public class VerifyRepository {
    @Resource
    private VerifyMapper verifyMapper;

    @Resource
    private ConfigInfo configInfo;

    /**
     * 按手机号和验证类型删除
     * @param mobile
     * @param type
     */
    public void deleteByMobileAndType(String mobile , byte type){
        VerifyExample verifyExample = new VerifyExample();
        VerifyExample.Criteria criteria = verifyExample.createCriteria();
        criteria.andTypeEqualTo(type).andMobileEqualTo(mobile);
        verifyMapper.deleteByExample(verifyExample);
    }

    /**
     * 插入验证码
     * @param verify
     */
    public void insert(Verify verify){
        verifyMapper.insertSelective(verify);
    }


    /**
     * 判断验证信息是否存在
     * @param mobile
     * @param code
     * @param type
     * @return
     */
    public boolean hasExistAndNotOverdue(String mobile , String code, byte type){
        Date now = new Date();
        Date overdueTime = new Date(now.getTime()-Integer.parseInt(configInfo.getVerifyTimeout())*1000);
        VerifyExample verifyExample = new VerifyExample();
        VerifyExample.Criteria criteria = verifyExample.createCriteria();
        criteria.andCodeEqualTo(code).andMobileEqualTo(mobile).andTypeEqualTo(type).andCreatetimeGreaterThan(overdueTime);
        return verifyMapper.countByExample(verifyExample)==1;
    }





}
